﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data;
using MYNAMESPACE;

namespace final_project
{
    public partial class Assin_Employee : Form
    {
        private connect_db con;
        public Assin_Employee()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void Execute_Click(object sender, EventArgs e)
        {
            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 

            
            
            employee em = new employee();
            if (ID.Text.Length == 0 || name.Text.Length == 0 || Password.Text.Length == 0 || Address.Text.Length == 0 || Mail.Text.Length == 0 || Phone.Text.Length == 0 || Salary.Text.Length == 0 || Job_Type.Text.Length == 0)
            {

                MessageBox.Show("must have value");


            }
            else
            {
                con = new connect_db();
                
                em.setnational_id(Int32.Parse(ID.Text.Trim()));
                em.setname(string.Format(name.Text.Trim()));
                em.setpassword(string.Format(Password.Text.Trim()));
                em.setaddress(string.Format(Address.Text.Trim()));
                em.setemail(string.Format(Mail.Text.Trim()));
                em.setgender(string.Format(Gender.Text.Trim()));
                em.setphone(long.Parse(Phone.Text.Trim()));
                em.setage(Int32.Parse(Age.Text.Trim()));
                em.setSalary(double.Parse(Salary.Text.Trim()));
                em.setkindofjob(string.Format(Job_Type.Text.Trim()));
                con.sqlquery("insert into Employee (id,name,pass,employee_add,mail,gender,phone,age,salary,job_type) values ('" + em.getnational_id()+ "','" + em.getname() + "','" + em.getpassword() + "','" + em.getaddress() + "','" + em.getmail() + "','" + em.getgender() + "','" + em.getphone() + "','" + em.getage() + "','" + em.getSalary() + "','" + em.getkindofjob() + "')");
                con.sqlnonex();
                MessageBox.Show("Done");
                this.Hide();
                Manager_Work f2 = new Manager_Work();
                f2.ShowDialog();
            }
        }
        private void ID_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(ID.Text))
            {

                e.Cancel = true;
                ID.Focus();
                errorProvider1.SetError(ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(ID, "Valid ID 4 Number");

            }



            else if (Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(ID, "");

            }
            else { }

        }

        private void name_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(name.Text))
            {

                e.Cancel = true;
                name.Focus();
                errorProvider1.SetError(name, "Name Must Be Entered !");

            }
            else if (Regex.IsMatch(name.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(name, "");

            }
            else
            {

            }

        }

        private void Password_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Password.Text))
            {

                e.Cancel = true;
                Password.Focus();
                errorProvider1.SetError(Password, "Password Must Be Entered !");
                //passworderror.SetError(Password_textBox.Text, "Password required!");
            }
            else if (!Regex.IsMatch(Password.Text, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Password, "Password Must more Than 8 Digits Contain upper-lowercase,numbers and special characters");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Password, "");
            }

            // Password Validation End 

        }

        private void Address_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Address.Text))
            {

                e.Cancel = true;
                Address.Focus();
                errorProvider1.SetError(Address, "Address  Must Be Entered !");

            }
            else if (Regex.IsMatch(Address.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(Address, "");

            }
            else
            {

            }

        }

        private void Mail_Validating(object sender, CancelEventArgs e)
        {


            if (string.IsNullOrEmpty(Mail.Text))
            {

                e.Cancel = true;
                Mail.Focus();
                errorProvider1.SetError(Mail, "Mail Must Be Entered !");

            }
            else if (!Regex.IsMatch(Mail.Text, @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Mail, "Address EX someone@example.com !");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Mail, "");
            }


        }

        private void Gender_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(Gender.Text))
            {

                e.Cancel = true;
                Gender.Focus();
                errorProvider1.SetError(Gender, "Gender Must Be Entered !");

            }
            else if (!Regex.IsMatch(Gender.Text, @"^[a-zA-Z]{1,5}$"))
            {
                e.Cancel = true ;
                errorProvider1.SetError(Gender, "Invalid !");

            }
            else
            {
                e.Cancel = false ;
                errorProvider1.SetError(Gender, "");

            }

        }

        private void Phone_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Phone.Text))
            {

                e.Cancel = true;
                Phone.Focus();
                errorProvider1.SetError(Phone, "Phoe Must Be Entered !");

            }
            else if (!Regex.IsMatch(Phone.Text, @"^\d{11}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Phone, "Enter 11 Numbers !");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Phone, "");
            }

        }

        private void Age_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Age.Text))
            {

                e.Cancel = true;
                Age.Focus();
                errorProvider1.SetError(Age, "Age Must Be Entered !");

            }
            else if (!Regex.IsMatch(Age.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Age, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Age, "");
            }

        }

        private void Job_Type_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(Job_Type.Text))
            {

                e.Cancel = true;
                Job_Type.Focus();
                errorProvider1.SetError(Job_Type, "Name Must Be Entered !");

            }
            else if (Regex.IsMatch(Job_Type.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(Job_Type, "");

            }
            else
            {

            }
        }

        private void Salary_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Salary.Text))
            {

                e.Cancel = true;
                Salary.Focus();
                errorProvider1.SetError(Salary, "Salary Must Be Entered !");

            }
            else if (!Regex.IsMatch(Salary.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Salary, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Salary, "");
            }

        }
    }
}
